package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusBpmnInfo;
import cn.wolfcode.business.mapper.BusBpmnInfoMapper;
import cn.wolfcode.business.service.BusBpmnInfoService;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class BusBpmnInfoServiceImpl implements BusBpmnInfoService {
    @Autowired
    private BusBpmnInfoMapper busBpmnInfoMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Override
    public List<BusBpmnInfo> list(BusBpmnInfo busBpmnInfo) {
        Map<String, Object> map = busBpmnInfo.getParams();
        if(map!=null){
            String endDeployTime = (String) map.get("endDeployTime");
            if(endDeployTime!=null){
                map.put("endDeployTime",endDeployTime+" 23:59:59");
            }
        }
        return busBpmnInfoMapper.selectBusBpmnInfoList(busBpmnInfo);
    }

    @Override
    public int add(MultipartFile file, Integer bpmnType, String info) throws IOException {
        if (file==null||bpmnType==null||info==null){
            throw new RuntimeException("非法参数");
        }
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        if(!("xml".equalsIgnoreCase(extension)||"bpmn".equalsIgnoreCase(extension))){
            throw new RuntimeException("文件格式不正确");
        }
        if (file.getSize()>1024*1024*10){
            throw new RuntimeException("文件大小不能超过5M");
        }
        Deployment deploy = repositoryService.createDeployment()
                .addInputStream(originalFilename, file.getInputStream())
                .deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        BusBpmnInfo busBpmnInfo = new BusBpmnInfo();
        busBpmnInfo.setBpmnLabel(originalFilename);
        busBpmnInfo.setBpmnType(bpmnType);
        busBpmnInfo.setProcessDefinitionKey(processDefinition.getKey());
        busBpmnInfo.setDeployTime(deploy.getDeploymentTime());
        busBpmnInfo.setVersion(processDefinition.getVersion());
        busBpmnInfo.setInfo(info);

        return busBpmnInfoMapper.add(busBpmnInfo);
    }

    @Override
    public InputStream readResource(Long id, String type) {
        if (id==null||type==null){
            throw new RuntimeException("非法参数");
        }
        BusBpmnInfo bpmnInfo = busBpmnInfoMapper.selectBusBpmnInFoId(id);
        if (bpmnInfo==null){
            throw new RuntimeException("非法参数");
        }
        String processDefinitionKey = bpmnInfo.getProcessDefinitionKey();
        Integer version = bpmnInfo.getVersion();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey)
                .processDefinitionVersion(version)
                .singleResult();
        if("xml".equalsIgnoreCase(type)){
            String resourceName = processDefinition.getResourceName();
            String deploymentId = processDefinition.getDeploymentId();
            return repositoryService.getResourceAsStream(deploymentId,resourceName);
        }
        if ("png".equalsIgnoreCase(type)){
            DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
            return generator.generateDiagram(
                    bpmnModel,
                    Collections.emptyList(),
                    Collections.emptyList(),
                    "宋体",
                    "宋体",
                    "宋体"
            );
        }

        return null;
    }

    @Override
    @Transactional
    public int remove(Long id) {
        if (id==null){
            throw new RuntimeException("非法参数");
        }
        BusBpmnInfo busBpmnInfo = busBpmnInfoMapper.selectBusBpmnInFoId(id);
        if (busBpmnInfo==null){
            throw new RuntimeException("非法参数");
        }
        String processDefinitionKey = busBpmnInfo.getProcessDefinitionKey();
        Integer version = busBpmnInfo.getVersion();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey)
                .processDefinitionVersion(version)
                .singleResult();
        repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true );
        return busBpmnInfoMapper.delete(id);
    }

    @Override
    public BusBpmnInfo selectType(Integer flowAuditType) {
        return busBpmnInfoMapper.selectType(flowAuditType);
    }
}
